package com.jsle.financial.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jsle.common.dao.mybatis.BaseDao;
import com.jsle.financial.domain.Node;
import com.jsle.financial.domain.User;
import com.jsle.financial.domain.Workflow;
import com.jsle.financial.util.Constants;
import com.jsle.financial.vo.Result;
import com.jsle.financial.vo.WorkflowAndNode;

@SuppressWarnings("unchecked")
public class WorkflowDaoImpl extends BaseDao implements WorkflowDao{
	private Log log = LogFactory.getLog(WorkflowDaoImpl.class);
	
	public Result saveAmountWorkflow(Workflow workflow){
		Result result = new Result();
		try {
			boolean workflowResult = insert("Workflow.insertWorkflow",workflow);
			if(workflowResult){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			log.error("saveAmountWorkflow error## "+ e.getMessage());
		}
		return result;
	}
	
	public Result saveAmountNode(Node node){
		Result result = new Result();
		try {
			boolean nodeResult = insert("Workflow.insertNode",node);
			if(nodeResult){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(nodeResult);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			log.error("saveAmountNode error## "+ e.getMessage());
		}
		return result;
	}
	
	public Result queryWorkflowByCode(String workflowCode){
		Result result = new Result();
		try {
			Workflow workflow = (Workflow)queryForObject("Workflow.queryWorkflowByCode", new Workflow(workflowCode));
			if(workflow!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(workflow);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			log.error("queryWorkflowByCode error## "+ e.getMessage());
		}
		return result;
	}
	
	public Result updateWorkflow(Workflow workflow){
		Result result = new Result();
		try {
			boolean ret = update("Workflow.updateWorkflow",workflow);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			log.error("queryWorkflowByCode error## "+ e.getMessage());
		}
		return result;
	}
	
	public Result queryWorkflowByUser(User user){
		Result result = new Result();
		try {
			List<Workflow> workflows = (List<Workflow>)queryForList("Workflow.queryWorkflowByUser", user);
			if(workflows!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(workflows);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			log.error("queryWorkflowByUser error## "+ e.getMessage());
		}
		return result;
	}
	public Result queryWorkflowBySelf(Workflow workflow){
		Result result = new Result();
		try {
			List<Workflow> workflows = (List<Workflow>)queryForList("Workflow.queryWorkflowBySelf", workflow);
			if(workflows!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(workflows);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			log.error("queryWorkflowByUser error## "+ e.getMessage());
		}
		return result;
	}
	public Result queryWorkflowAndNodes(Workflow workflow){
		Result result = new Result();
		try {
			List<WorkflowAndNode> workflowAndNode = (List<WorkflowAndNode>)queryForList("Workflow.queryWorkflowAndNodes", workflow);
			if(workflowAndNode!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			log.error("queryWorkflowAndNodes error## "+ e.getMessage());
		}
		return result;
	}
	
	public Result queryNodes(Workflow workflow){
		Result result = new Result();
		try {
			List<Node> nodes = (List<Node>)queryForList("Workflow.queryNodes", workflow);
			if(nodes!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(nodes);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			log.error("queryNodes error## "+ e.getMessage());
		}
		return result;
	}
	
	public Result updateNode(Node node){
		Result result = new Result();
		String mapperUrl =  "Workflow.updateNode2";
		if(node.getNodeCode()==3){
			mapperUrl =  "Workflow.updateNode3";
		}
		try {
			boolean ret = update(mapperUrl,node);
			if(ret){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(ret);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			log.error("updateNode error## "+ e.getMessage());
		}
		return result;
	}

	public Result queryWorkflowByWorkflow(Workflow workflow) {
		Result result = new Result();
		try {
			List<Workflow> workflows = (List<Workflow>)queryForList("Workflow.queryWorkflowByWorkflow", workflow);
			if(workflows!=null){
				result.setResultCode(Constants.FC_RESULT_SUCCESS_CODE);
				result.setResultMessage(Constants.FC_RESULT_SUCCESS_MESSAGE);
				result.setResultValue(workflows);
			}else{
				result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
				result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			}
		} catch (Exception e) {
			result.setResultCode(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_CODE);
			result.setResultMessage(String.format(Constants.Dao.FC_RESULT_INSERT_DAO_ERROR_MESSAGE, "User"));
			log.error("queryWorkflowByWorkflow error## "+ e.getMessage());
		}
		return result;
	}
}
